import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:跳跃分组使大的数据尽量放在后面
 * User: 东莞呵呵
 * Date:2022-06-19
 * Time:21:28
 */
public class ShellSort {

    public static void Shell(int[] nums, int gap) {
        for (int i = gap; i < nums.length; i++) {
            int tmp = nums[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (tmp < nums[j]) {
                    nums[j + gap] = nums[j];//这里的i与j+gap不一样注意！！
                } else {
                    break;
                }
            }
            nums[j + gap] = tmp;
        }
    }

    public static int[] ShellSort(int[] nums) {
        int gap = nums.length;
        while (gap > 1) {
            Shell(nums, gap);
            gap /= 2;
        }
        Shell(nums, 1);
        return nums;
    }

    public static void main(String[] args) {
        int[] arr = {65, 43, 5, 7, 4, 5, 76, 23, 4, 765, 42, 6, 7, 8};
        int[] ret = ShellSort(arr);
        System.out.println(Arrays.toString(ret));
    }
}
